<?php
class ome_return_process{
	
	function do_iostock($por_id,$io,&$msg){
    	//生成出入库明细
		$allow_commit = false;
        kernel::database()->exec('begin');
        $iostockData = $this->get_iostock_data($por_id);
        foreach ($iostockData as $iostock){
            
            if(kernel::single('siso_receipt_iostock_reship')->create($iostock, $data, $msg)){
                $allow_commit = true;
            }else{
                $allow_commit = false;
            }
        }
        if ($allow_commit == true){
            kernel::database()->commit();
            return true;
        }else{
            kernel::database()->rollBack();
            $msg = $iostock_msg;
            return false;
        }
    }
    
/**
     * 根据仓库组织出库数据
     * @access public
     * @param String $iso_id 出入库ID
     * @return sdf 出库数据
     */
    public function get_iostock_data($pro_id){
        $pro_items_detail = $this->getProItems($pro_id);
        $iostock_data = array();
        $pro_detail = $this->getRetrunProcess($pro_id);
        $reship_id = $pro_detail['reship_id'];
        if ($pro_items_detail){
            foreach ($pro_items_detail as $k=>$v){
                $iostock_data[$v['branch_id']]['items'][$v['item_id']] = array(
                    'item_id' => $v['item_id'],
                    'bn' => $v['bn'],
                    'price' => $v['need_money'],
                    'normal_num' => $v['num'],
                    'create_time' => time(),
                );
                $iostock_data[$v['branch_id']]['reship_id'] = $reship_id;
                $iostock_data[$v['branch_id']]['branch_id'] = $v['branch_id'];
                $iostock_data[$v['branch_id']]['memo'] = $pro_detail['memo'];
            }
        }
       
        return $iostock_data;
    }
    
	function getProItems($pro_id){
		$objProItems = app::get('ome')->model('return_process_items');
		$pro_items_detail = $objProItems->getList('*', array('por_id'=>$pro_id), 0, -1);
        
		return $pro_items_detail;

    }
    
    function getRetrunProcess($pro_id,$field='*'){
    	$db = kernel::database();
        $sql = 'SELECT '.$field.' FROM `sdb_ome_return_process` WHERE `por_id`=\''.$pro_id.'\'';
        return $db->selectrow($sql);
    }

}
